import {_decorator, clamp, Component, tween, UITransform} from 'cc';
import {GameManager} from "db://assets/scripts/game/GameManager";
import {EventType} from "db://assets/scripts/common/EventType";
import {EventManager} from "db://assets/scripts/common/EventManager";
import {BossController} from "db://assets/scripts/game/role/boss/BossController";

const {ccclass, property} = _decorator;

/**
 * boss血条脚本
 */
@ccclass('BossBloodBarController')
export class BossBloodBarController extends Component {
    /**
     * 白色进度条的UI组件
     */
    @property(UITransform)
    public whiteBarTF: UITransform;
    /**
     * 血量进度条的UI组件
     */
    @property(UITransform)
    public redBarTF: UITransform;

    /**
     * 白色进度条高度
     */
    private whiteBarHeight: number = 28;
    /**
     * 血量进度条高度
     */
    private redBarHeight: number = 28;
    /**
     * 总的血量
     */
    private totalBlood: number = 0;
    /**
     * 当前血量值
     */
    private currentBlood: number = 0;
    /**
     * 当前boss血条中白色进度条长度
     */
    private bossMaxWhiteBarWidth: number = 560;
    /**
     * 当前boss血条中血量条长度
     */
    private bossMaxRedBarWidth: number = 560;
    /**
     * 血条是否为空
     */
    private isBloodEmpty: boolean = false;

    onEnable() {
        EventManager.on(EventType.REFRESH_BOSS_BLOOD, this.refreshBossBlood, this);
    }

    onDisable() {
        EventManager.off(EventType.REFRESH_BOSS_BLOOD, this.refreshBossBlood, this);
    }

    /**
     * 展示血条
     *
     * @param scriptParent 血条使用者
     * @param totalBlood 总血量
     * @param isInit 是否是初次展示，初次展示则显示完整血量，否则刷新的时候展示当前血量
     */
    public show(scriptParent: BossController, totalBlood: number, isInit: boolean = true) {
        this.node.active = true;

        this.totalBlood = totalBlood;
        this.isBloodEmpty = false;

        if (isInit) {
            this.currentBlood = this.totalBlood;
        }

        //当前血量占全部的比例
        let ratio = this.currentBlood / this.totalBlood;
        ratio = clamp(ratio, 0, 1);

        //进度条宽度设置
        this.whiteBarTF.setContentSize(ratio * this.bossMaxWhiteBarWidth, this.whiteBarHeight);
        this.redBarTF.setContentSize(ratio * this.bossMaxRedBarWidth, this.redBarHeight);
    }

    /**
     * 刷新boss血量
     *
     * @param blood 血量值
     */
    private refreshBossBlood(blood: number) {
        this.currentBlood += blood;

        let ratio = this.currentBlood / this.totalBlood;
        ratio = ratio <= 0 ? 0 : ratio;

        if (blood < 0) {//减血
            this.redBarTF.setContentSize(this.bossMaxRedBarWidth * ratio, this.whiteBarHeight);

            if (!this.isBloodEmpty) {
                this.isBloodEmpty = ratio <= 0;

                tween(this.whiteBarTF)
                    .to(0.7, {width: this.bossMaxWhiteBarWidth * ratio})
                    .call(() => {
                        if (ratio <= 0) {
                            this.node.active = false;
                            GameManager.getInstance().gameBossProxy.getBossController().setIsDie(true);
                        }
                    })
                    .start();
            }
        }
    }
}

